package com.wooophone.webapp.mpyeah.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;
import com.wooophone.common.web.action.BaseAction;
import com.wooophone.mpyeah.biz.BrandBiz;
import com.wooophone.mpyeah.biz.BrandCommentBiz;
import com.wooophone.mpyeah.biz.OnLineStoreBiz;
import com.wooophone.mpyeah.entity.Brand;
import com.wooophone.mpyeah.entity.BrandComment;
import com.wooophone.mpyeah.entity.OnLineStore;

public class BrandAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Autowired
	private BrandBiz brandBiz;

	@Autowired
	private BrandCommentBiz brandCommentBiz;

	@Autowired
	private OnLineStoreBiz onLineStoreBiz;

	private Brand brand;

	private List<BrandComment> brandComments;

	private List<Brand> list;
	private List<Brand> top10List;

	private String keyword;

	private List<OnLineStore> top5OnLineStore;
	List<OnLineStore> top30OnlineStore;

	@RequiredStringValidator(fieldName = "id", key = "id.null", message = "未提供ID")
	public String view() {
		try {
			brand = brandBiz.get(id);

			if (brand == null)
				return "null";

			Map<String, Object> parameter = new HashMap<String, Object>();
			parameter.put("brandId", id);

			pager.setRecordCount(brandCommentBiz.count(parameter));
			brandComments = brandCommentBiz.find(parameter,
					(pager.getCurrPageNo() - 1) * pager.getPageSize(),
					pager.getPageSize());
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}

	/**
	 * 搜索
	 * 
	 * @return
	 */
	public String search() {
		try {
			pager.setRecordCount(brandBiz.count(keyword));
			list = brandBiz.search(keyword,
					(pager.getCurrPageNo() - 1) * pager.getPageSize(),
					pager.getPageSize());

			/* Top10 */
			top10List = brandBiz.findRecommendBrand(0, 5);

			top30OnlineStore = onLineStoreBiz.recommendOnLineStore(0, 30);

			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}

	@SuppressWarnings("unchecked")
	public String list() {
		try {
			if (id == null || "".equals(id)) {
				pager.setRecordCount(brandBiz.count());
				list = brandBiz.list(
						(pager.getCurrPageNo() - 1) * pager.getPageSize(),
						pager.getPageSize());
			} else {
				// 得到列表，并更新Count
				Map<String, Object> map = brandBiz
						.findBrandsAndChildBrandByCid(
								id,
								(pager.getCurrPageNo() - 1)
										* pager.getPageSize(),
								pager.getPageSize());
				pager.setRecordCount(Integer.valueOf(map.get(
						BrandBiz.KEY_BRAND_LIST_COUNT).toString()));
				list = (List<Brand>) map.get(BrandBiz.KEY_BRAND_LIST_BRANDS);
			}
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}

	public String hots() {

		list = brandBiz.findRecommendBrand(0, 30);

		/* Top10 */
		Map<String, Object> parameter = new HashMap<String, Object>();
		parameter.put("level", 5);
		top5OnLineStore = onLineStoreBiz.find(parameter, 0, 5);
		top30OnlineStore = onLineStoreBiz.recommendOnLineStore(5, 35);
		return SUCCESS;
	}

	public void setBrandBiz(BrandBiz brandBiz) {
		this.brandBiz = brandBiz;
	}

	public Brand getBrand() {
		return brand;
	}

	public void setBrand(Brand brand) {
		this.brand = brand;
	}

	public List<Brand> getList() {
		return list;
	}

	public void setList(List<Brand> list) {
		this.list = list;
	}

	public void setBrandCommentBiz(BrandCommentBiz brandCommentBiz) {
		this.brandCommentBiz = brandCommentBiz;
	}

	public List<BrandComment> getBrandComments() {
		return brandComments;
	}

	public void setBrandComments(List<BrandComment> brandComments) {
		this.brandComments = brandComments;
	}

	public String getKeyword() {
		return keyword;
	}

	public void setKeyword(String keyword) {
		this.keyword = keyword;
	}

	public List<Brand> getTop10List() {
		return top10List;
	}

	public void setTop10List(List<Brand> top10List) {
		this.top10List = top10List;
	}

	public List<OnLineStore> getTop30OnlineStore() {
		return top30OnlineStore;
	}

	public void setTop30OnlineStore(List<OnLineStore> top30OnlineStore) {
		this.top30OnlineStore = top30OnlineStore;
	}

	public void setOnLineStoreBiz(OnLineStoreBiz onLineStoreBiz) {
		this.onLineStoreBiz = onLineStoreBiz;
	}

	public List<OnLineStore> getTop5OnLineStore() {
		return top5OnLineStore;
	}

	public void setTop5OnLineStore(List<OnLineStore> top5OnLineStore) {
		this.top5OnLineStore = top5OnLineStore;
	}
}
